<?php

include_once './database.php';
include_once './arrarr_to_arr.php';
include_once 'log_text.php';
include_once './get_param.php';

function fetch_expiry_data($dismiss_notify = false) {
  log_text('fetch_expiry_data');

  $params = get_param('expiryChecking');
  // function new !
  $names = array();
  $output = array('groups' => array(), 'phones' => array(), 'names' => array());
  // get numbers (disposed = 0 and qywxNotify = 1)
  $sql = "SELECT `number` FROM `oprec_security_data` WHERE `disposed` = 0";
  $client_number_list_raw = exec_sql($sql);
  $client_number_list = implode(',', arrarr_to_arr($client_number_list_raw, 'number'));
  // peocess with each threshold
  foreach ($params as $item) {
    if ($item->enable && ($dismiss_notify || $item->notify)) {
      //
      $item_title = $item->key;
      $threshold_days = $item->days;
      $threshold_date = date('Y-m-d', time() + $threshold_days * 86400);
      $blacklist_arr = implode(',', $item->blacklist);
      $ignored_arr = implode(',', $item->ignored);
      $content = array();
      $count = 0;
      // get names matched conditions
      $sql = "SELECT `clientName` FROM `pims_extention_data` WHERE `itemTitle` = '{$item_title}' AND `itemValue` <= '{$threshold_date}' AND `status` = 9 AND `number` IN ({$client_number_list})";
      if (count($item->blacklist) > 0) {
        $sql .= " AND `number` NOT IN ($blacklist_arr)";
      }
      if (!$dismiss_notify && count($item->ignored) > 0) {
        $sql .= " AND `number` NOT IN ($ignored_arr)";
      }
      $sql .= " ORDER BY `itemValue` ASC";
      $content_names = exec_sql($sql);
      // check data: if matched names existed
      if ($content_names) {
        // merge matched names to full names list
        $output['names'] = array_merge($output['names'], arrarr_to_arr($content_names, 'clientName'));
        // get values matched conditions
        $sql = "SELECT `clientName`, `itemValue` FROM `pims_extention_data` WHERE `itemTitle` = '{$item_title}' AND `itemValue` <= '{$threshold_date}' AND `status` = 9 AND `number` IN ({$client_number_list})";
        if (count($item->blacklist) > 0) {
          $sql .= " AND `number` NOT IN ($blacklist_arr)";
        }
        if (!$dismiss_notify && count($item->ignored) > 0) {
          $sql .= " AND `number` NOT IN ($ignored_arr)";
        }
        $sql .= "ORDER BY `itemValue` ASC";
        $content = exec_sql($sql);
        $count = count($content);
      }
      // puush to output array
      array_push(
        $output['groups'],
        array(
          'itemTitle' => $item_title,
          'thresholdDays' => $threshold_days,
          'thresholdDate' => $threshold_date,
          'count' => $count,
          'content' => $content,
        )
      );
    }
  }
  // dedup of names list and convert to string
  $output['names'] = array_unique($output['names']);
  $matched_name_list = implode("','", $output['names']);
  // get phone list as `mentioned_phone_list`
  $sql = "SELECT `itemValue` FROM `pims_extention_data` WHERE `itemTitle` = '手机号' AND `clientName` IN ('{$matched_name_list}') AND `status` = 9 ORDER BY `number` ASC";
  $res = exec_sql($sql);
  if ($res) {
    $output['phones'] = arrarr_to_arr($res, 'itemValue');
  }

  // var_dump($output);
  return $output;
}

/*----------------------------------------------------------------

output: {
names: [],
phones: [],
content: [
{
itemTitle: '',
thresholdDays: 60,
thresholdDate: '',
count: 0,
content: [
{
clientName: '',
itemValue: ''
}
]
}
]
}

----------------------------------------------------------------*/